<?php

/**
 * This is the model class for table "{{slider}}".
 *
 * The followings are the available columns in table '{{slider}}':
 * @property integer $id
 * @property string $name_title
 * @property string $title
 * @property string $photo
 * @property string $content
 * @property string $link
 * @property integer $status
 * @property integer $created_at
 * @property integer $language_id
 * @property integer $type
 * @property integer $rank
 */
class Slider extends MyARContent
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Slider the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{slider}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name_title , photo', 'required'),
            array('status, created_at, language_id, type, rank', 'numerical', 'integerOnly' => true),
            array('name_title, title, photo', 'length', 'max' => 255),
            array('link, title, content', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name_title, title, photo, content, link, status, created_at, language_id, type, rank', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.

        $relations = parent::relations();
        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'          => 'ID',
            'name_title'  => 'Tên ảnh',
            'title'       => 'Tiêu đề',
            'photo'       => 'Ảnh',
            'content'     => 'Mô tả',
            'link'        => 'Đường link web',
            'status'      => 'Trạng thái',
            'created_at'  => 'Created At',
            'language_id' => 'Language',
            'type'        => 'Type',
            'rank'        => 'Thứ tự',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name_title', $this->name_title, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('photo', $this->photo, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('link', $this->link, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('created_at', $this->created_at);
        $criteria->compare('language_id', $this->language_id);
        $criteria->compare('type', $this->type);
        $criteria->compare('rank', $this->rank);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function getAllPictureSlier($limit = 10)
    {
        $cri            = new CDbCriteria();
        $cri->condition = 'status>0';
        $cri->order     = 'rank ASC, id DESC';
        $cri->limit     = $limit;
        return self::model()->findAll($cri);
    }

    public function getThumb()
    {
        return $this->photo;
    }

}
